package com.example.securitylianxi.util;

import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Component
public class JwtUtil {

    public static String createJWT(String userId, Long ttlSend) {
        long expMillis = System.currentTimeMillis() + ttlSend * 1000L;
        String token = Jwts.builder().setId(UUID.randomUUID().toString().replaceAll("-", ""))
                .setId(UUID.randomUUID().toString())
                .setSubject(userId)
                .setIssuer("ZJB")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, "rukoujihua")
                .setExpiration(new Date(expMillis)).compact();
        return token;
    }

    public static Claims parseJWT(String token) {
        Claims claims;
        try {
            claims = (Claims) Jwts.parser().setSigningKey("rukoujihua")
                    .parse(token).getBody();
        } catch (ExpiredJwtException e) {
            claims = e.getClaims();
        }
        return claims;
    }
}
